import global from "@/dict/global";
import {useAdminStore} from "@/store/admin";
import router from "@/router";

const tokenKey = global.adminToken

export function getAdminToken(){
    return localStorage.getItem(tokenKey)
}

export function getToken(adminToken: string) {
    // 使用localStorage存储
    return localStorage.getItem(tokenKey)
}

export function setToken(key:string, value:string) {
    return localStorage.setItem(tokenKey, value)
}

export function removeToken(key:string) {
    return localStorage.removeItem(tokenKey)
}

// 生成指定模型对应的api请求路径（于权限对应，也可用于权限验证）
// export const makeModelUrl = (model:string) => {
//     return {
//         index: `/${model}/index`,
//         add: `/${model}/add`,
//         edit: `/${model}/edit`,
//         del: `/${model}/del`,
//         export: `/${model}/export`,
//         password: `/${model}/password`,
//         rule: `/${model}/rule`,
//         init: `/${model}/init`,
//     }
// }

export const makeModelAuth = (model:string) => {
    return {
        index: `${model}/index`,
        add: `${model}/add`,
        edit: `${model}/edit`,
        del: `${model}/del`,
        export: `${model}/export`,
        password: `${model}/password`,
        rule: `${model}/rule`,
        init: `${model}/init`,
    }
}

export const checkAuth = (auth:string) => {

    const adminStore = useAdminStore()
    let auths = adminStore.rules
    return auths.some(item => item.toLowerCase() == auth.toLowerCase())
}

export const logout = () => {
    // 退出登录
    removeToken(global.adminToken)
    router.push("/login")
}

